package com.neusoft.elmboot.controller;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.neusoft.elmboot.Dto.OrdersDto;
import com.neusoft.elmboot.Vo.BussinessVo;
import com.neusoft.elmboot.Vo.FoodVo;
import com.neusoft.elmboot.Vo.OrdersVo;
import com.neusoft.elmboot.common.result.Result;
import com.neusoft.elmboot.po.*;
import com.neusoft.elmboot.service.*;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.time.LocalDateTime;
import java.util.ArrayList;
import java.util.List;
import java.util.stream.Collectors;

@RequestMapping("/orders")
@RestController
@Slf4j
@Api(tags = "订单管理")
public class OrderController {

    @Autowired
    private OrdersService ordersService;
    @Autowired
    private OrderdetailService orderdetailService;

    @Autowired
    private BusinessService businessService;

    @Autowired
    private FoodService foodService;

    @Autowired
    private CartService cartService;

    @GetMapping("/page")
    @ApiOperation("订单分页查询")
    public Result<Page> page(Integer page,Integer limit,String orderId,String userId){
        log.info("订单分页查询,{},{},{},{}",page,limit,orderId,userId);
        Page pageInfo = new Page(page,limit);
        LambdaQueryWrapper<OrdersEntity> lqw = new LambdaQueryWrapper<>();
        lqw.eq(orderId != null, OrdersEntity::getOrderId, orderId);
        lqw.eq(userId != null, OrdersEntity::getUserId, userId);
        ordersService.page(pageInfo, lqw);
        return Result.success(pageInfo);
    }



    @PostMapping("/getByUserId")
    public Result<List<OrdersVo>> getByUserId(Long userId){

        List<OrdersVo> ordersVoList = ordersService.getByUserId(userId);
//        LambdaQueryWrapper<OrdersEntity> lambdaQueryWrapper = new LambdaQueryWrapper<>();
//        lambdaQueryWrapper.eq(OrdersEntity::getUserId, userId);
//        OrdersEntity OrdersEntity = ordersService.getOne(lambdaQueryWrapper);
//
//        OrdersVo ordersVo = new OrdersVo();
//        BeanUtils.copyProperties(OrdersEntity,ordersVo);
        return Result.success(ordersVoList);
    }

    /**
     * 订单支付
     * @return
     */
    @ApiOperation("订单支付")
    @PostMapping
    public Result payment(@RequestBody OrdersDto ordersDto){
        log.info("订单支付,{}",ordersDto);
        OrdersEntity ordersEntity = new OrdersEntity();
        BeanUtils.copyProperties(ordersDto,ordersEntity);
        ordersEntity.setOrderState(0);
        ordersEntity.setOrderDate(LocalDateTime.now());
        ordersService.save(ordersEntity);
        LambdaQueryWrapper<CartEntity> lqw = new LambdaQueryWrapper();
        lqw.eq(CartEntity::getUserId,ordersEntity.getUserId());
        lqw.eq(CartEntity::getBusinessId,ordersEntity.getBusinessId());
        List<CartEntity> cartEntityList = cartService.list(lqw);
        for (CartEntity item : cartEntityList) {
            OrderdetailetEntity orderdetailetEntity = new OrderdetailetEntity();
            orderdetailetEntity.setFoodId(item.getFoodId());
            orderdetailetEntity.setQuantity(item.getQuantity());
            orderdetailetEntity.setOrderId(ordersEntity.getOrderId());
            orderdetailService.save(orderdetailetEntity);
        }


        LambdaQueryWrapper<CartEntity> lambdaQueryWrapper = new LambdaQueryWrapper<>();
        lambdaQueryWrapper.eq(CartEntity::getUserId,ordersEntity.getUserId());
        cartService.remove(lambdaQueryWrapper);
//        OrdersVo ordersVo = new OrdersVo();
//        ordersVo.setOrderId(ordersEntity.getOrderId());
        return Result.success(ordersEntity.getOrderId());
    }

    /**
     * 根据订单id查询订单
     * @param orderId
     * @return
     */
    @ApiOperation("根据订单id查询订单")
    @GetMapping("/getOrdersById")
    public Result<OrdersVo> getById(Long orderId){
        log.info("根据订单id查询订单,{}",orderId);
        OrdersEntity ordersEntity = ordersService.getById(orderId);
        BusinessEntity businessEntity = businessService.getById(ordersEntity.getBusinessId());
        BussinessVo bussinessVo = new BussinessVo();
        BeanUtils.copyProperties(businessEntity,bussinessVo);
        LambdaQueryWrapper<OrderdetailetEntity> lqwO = new LambdaQueryWrapper<>();
        lqwO.eq(OrderdetailetEntity::getOrderId, orderId);
        List<Integer> list = new ArrayList<>();
        List<OrderdetailetEntity> orderdetailetEntityList = orderdetailService.list(lqwO);
        for (OrderdetailetEntity orderdetailetEntity : orderdetailetEntityList) {

            if (!list.contains(orderdetailetEntity.getFoodId())){
                list.add(orderdetailetEntity.getFoodId());
            }
        }
        List<FoodVo> foodVoList = new ArrayList<>();
        for (Integer foodId : list) {
            FoodEntity foodEntity = foodService.getById(foodId);
            FoodVo foodVo = new FoodVo();
            BeanUtils.copyProperties(foodEntity,foodVo);
            foodVoList.add(foodVo);
        }



        OrdersVo ordersVo = new OrdersVo();
        BeanUtils.copyProperties(ordersEntity,ordersVo);
        ordersVo.setBussinessVo(bussinessVo);
        ordersVo.setFoodVo(foodVoList);




        return Result.success(ordersVo);
    }

    /**
     * 改变订单状态1
     * @param ordersDto
     * @return
     */
    @PutMapping
    public Result changeStatus(@RequestBody OrdersDto ordersDto){
        LambdaQueryWrapper<OrdersEntity> lqw = new  LambdaQueryWrapper();
        lqw.eq(OrdersEntity::getOrderId,ordersDto.getOrderId());
        OrdersEntity ordersEntity = ordersService.getOne(lqw);
        ordersEntity.setOrderState(1);
        ordersService.updateById(ordersEntity);
        return Result.success();


    }

    @GetMapping("/editStatus")
    public Result editStatus(Long id){

        OrdersEntity ordersEntity = ordersService.getById(id);
        ordersEntity.setOrderState(1);
        ordersService.updateById(ordersEntity);
        return Result.success();

    }






}
